# Work in progress - can be used for building cross-platform test app.
# Eventually want to add support for building and signing the main
# application, and generating documentation etc.

# Application name and version number
APPNAME 	= Simplenote
MAJOR_VERSION	= 1
MINOR_VERSION	= 0
BUILD_VERSION	= $(shell svnversion -n)
VERSION_STRING  = $(MAJOR_VERSION).$(MINOR_VERSION).$(BUILD_VERSION)
ZIP		= $(APPNAME)-BlackBerry-$(VERSION_STRING).zip

# Location for the JDE used to build and sign the application
JDE 		= $(PROGRAMFILES)/Research In Motion/BlackBerry JDE 4.5.0
RAPC 		= "$(JDE)/bin/rapc.exe"
BB_API 		= "$(JDE)/lib/net_rim_api.jar"

# Used for signing the code before it can be run on device -
# SIGTOOL_PASSWORD must be defined in the environment.  The flags used
# for the command are:
#
#  -a automatically request signatures
#  -c close if no errors requesting signatures
#  -s display number of signatures requested and signed
#  -p <password> for the code signing keys
SIGTOOL	= java -jar "$(JDE)/bin/SignatureTool.jar" \
               -a -c -s -p $(SIGTOOL_PASSWORD)

# Source, resources, and images used for the BlackBerry specific parts
# of the Simplenote application.
BB_SRC	   = $(wildcard src/com/simplenoteapp/blackberry/*.java) \
             $(wildcard src/com/simplenoteapp/blackberry/*.rr[ch]) \
             $(wildcard src/com/simplenoteapp/blackberry/*.png) \
             src/simplenote-icon.png

# Source code for the cross-platform Simplenote API
SN_SRC	   = $(wildcard src/com/simplenoteapp/simplenote/*.java)

# Source code for the cross platform test application
TEST_SRC   = $(wildcard src/com/simplenoteapp/test/*.java)

# Apache Commons code used for the cross platform test application
APACHE_SRC = $(wildcard src/org/apache/commons/codec/*.java) \
	     $(wildcard src/org/apache/commons/codec/binary/*.java)

# Build the Simplenote application and test code
all:	simplenote test

# Build, sign, and package the Simplenote application.  The
# SIGTOOL_PASSWORD environment variable should be set to the password
# used to sign the code.  Injects the application version number into
# the JAD file and attempts to disable touch compatibility mode for
# touchscreen devices.
simplenote: $(BB_SRC) $(SN_SRC)
ifndef SIGTOOL_PASSWORD
	$(error SIGTOOL_PASSWORD not set in environment)
endif
	$(RAPC) -quiet import=$(BB_API) codename=$(APPNAME) $(SN_SRC) $(BB_SRC)
	cp $(APPNAME).jad $(APPNAME).jad.tmp
	sed "s/MIDlet-Version: 0.0/MIDlet-Version: $(VERSION_STRING)/" $(APPNAME).jad.tmp > $(APPNAME).jad
	echo "RIM-TouchCompatibilityMode: false" >> $(APPNAME).jad
	$(SIGTOOL) $(APPNAME).cod
	zip $(ZIP) $(APPNAME).jad $(APPNAME).cod

# Build the test code.  This runs natively on the desktop and so
# doesn't require signing.
test: $(APACHE_SRC) $(SN_SRC) $(TEST_SRC)
	mkdir -p bin
	javac -d bin $(APACHE_SRC) $(SN_SRC) $(TEST_SRC)
	jar cf0e test.jar com.simplenoteapp.test.Test -C bin .
	@echo -e '\nRun with "java -jar test.jar command email password".'

# Remove Simplenote app from the current directory and the src
# directory as the Makefile leaves the artefacts here, and the IDE
# leaves them in the src directory.
clean:
	rm -f $(APPNAME).{cod,csl,cso,debug,jad,jad.tmp,jar} $(ZIP)
	rm -f src/$(APPNAME).{cod,csl,cso,debug,jad,jar}
	rm -rf bin test.jar

